<?php

require_once dirname(__FILE__) . '/../lib/bb_reserva_libroGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/bb_reserva_libroGeneratorHelper.class.php';

/**
 * bb_reserva_libro actions.
 *
 * @package    ingenyo-biblio
 * @subpackage bb_reserva_libro
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class bb_reserva_libroActions extends autoBb_reserva_libroActions
{

  public function executeNew(sfWebRequest $request)
  {
    $id_documento = $this->getUser()->getAttribute('documento');
    $this->form = new BbReservaLibroForm(null, array('idDocumento' => $id_documento));
    $this->BbReservaLibro = $this->form->getObject();
    $this->setLayout('layout_iframe');

  }

  public function executeCreate(sfWebRequest $request)
  {
    $id_documento = $this->getUser()->getAttribute('documento');
    $this->form = new BbReservaLibroForm(null, array('idDocumento' => $id_documento));
    $this->processForm($request, $this->form);
    $this->BbReservaLibro = $this->form->getObject();
    $this->setTemplate('new');
    $this->setLayout('layout_iframe');

  }

  public function executeShow(sfWebRequest $request)
  {
    $this->nuevo = $this->getUser()->hasAttribute('nuevo');
    $this->getUser()->clearAttribute('nuevo');
    parent::executeShow($request);
    $this->setLayout('layout_iframe');

  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'La reserva se ha realizado con exito.' : 'La reserva a sido actualizada.';

      $reserva = $form->save();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $reserva)));

      $this->getUser()->setFlash('notice', $notice,false);
      $persona = $reserva->getPersona();

      if (!is_null($persona->getMail()))
      {
        $message = $this->getMailer()->compose();
        $message->setSubject('UCALP Reserva de libro');
        $message->setTo($persona->getMail());
        $message->setFrom(sfConfig::get('app_mail'));
        $message->setBody('Se realizo la reserva del libro ' . $reserva->getLibro() . ' para la fecha ' . $reserva->getFechavencimiento('d-m-Y'), 'text/html');
        $this->getMailer()->send($message);
      }
      $this->getUser()->setAttribute('nuevo',true);
      $this->redirect(array('sf_route' => 'bb_reserva_libro_show', 'sf_subject' => $reserva));
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
    }

  }

  public function executeRetirar(sfWebRequest $request)
  {
    $this->BbPrestamo = $this->getRoute()->getObject()->retirar();
    $this->getUser()->setFlash('notice', 'La reserva se concreto con exito y el correspondiente prestamo fue creado.',false);

    $this->getUser()->setAttribute('nuevo',true);
      $this->redirect('bb_prestamo/show?id='.$this->BbPrestamo->getid());
    
     # $this->redirect('bb_reserva_libro/'.$this->getUser()->getAttribute('action'));
    
    

  }

  public function executeCancelar(sfWebRequest $request)
  {
    $this->getRoute()->getObject()->cancelar();
    $this->getUser()->setFlash('notice', 'La reserva se cancelo y el libro paso a estado activo.',false);
    $this->redirect('bb_reserva_libro/'.$this->getUser()->getAttribute('action'));

  }

  public function executeIndexByPersona(sfWebRequest $request)
  {
    parent::executeIndex($request);
    $this->getUser()->setAttribute('action','indexByPersona');
    $this->pager->setCriteria(BbreservalibroPeer::getVigentesByPersonaCriteria($this->getUser()->getAttribute('id_persona')));
    $this->pager->init();
    $this->setTemplate('index');

  }
  public function executeIndexByDocumento(sfWebRequest $request)
  {
    parent::executeIndex($request);
    $this->getUser()->setAttribute('action','indexByDocumento');
    $this->pager->setCriteria(BbreservalibroPeer::getVigentesByDocumentoCriteria($this->getUser()->getAttribute('id_documento')));
    $this->pager->init();
    $this->setTemplate('index');
    $this->setLayout('layout_iframe');
  }
  

}
